﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media;

namespace SqlMiM.Common.Controls
{
    /// <summary>
    /// Thumb 
    /// </summary>
    public class TitleBarThumb : Thumb
    {
        private Window parentWindow = null;
        
        
        public Boolean CanChangeWindowState { get; set; }

        /// <summary>
        /// Title bar
        /// </summary>
        public String Title
        {
            get { return (String)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(String), typeof(TitleBarThumb), new UIPropertyMetadata(String.Empty));

        public readonly static RoutedUICommand CloseCommand = new RoutedUICommand("Close", "CloseCommand", typeof(TitleBarThumb));



        /// <summary>
        /// Title Margin
        /// </summary>
        public Thickness TitleMargin
        {
            get { return (Thickness)GetValue(TitleMarginProperty); }
            set { SetValue(TitleMarginProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TitleMargin.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleMarginProperty =
            DependencyProperty.Register("TitleMargin", typeof(Thickness), typeof(TitleBarThumb), new UIPropertyMetadata(new Thickness(10,0,0,0)));



        public ImageSource IconSource
        {
            get { return (ImageSource)GetValue(IconSourceProperty); }
            set { SetValue(IconSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconSourceProperty =
            DependencyProperty.Register("IconSource", typeof(ImageSource), typeof(TitleBarThumb), new UIPropertyMetadata(null));


      

        protected Window Owner
        {
            get
            {
                if (parentWindow == null)
                    parentWindow = Window.GetWindow(this);

                return parentWindow;
            }
        }

        public TitleBarThumb()
        {
            this.CommandBindings.Add(new CommandBinding(CloseCommand, OnCloseCommand));

            //CommandManager.RegisterClassCommandBinding(typeof(TitleBarThumb), new CommandBinding(CloseCommand, OnCloseCommand));
            //CommandManager.RegisterClassInputBinding(typeof(TitleBarThumb), new InputBinding(CloseCommand, new KeyGesture(Key.F5)));


            this.CanChangeWindowState = true;
            this.DragDelta += OnDragDelta;
        }

        void OnCloseCommand(object sender, ExecutedRoutedEventArgs e)
        {
            if (this.Owner != null)
                this.Owner.Close();
        }

        protected override void OnMouseDoubleClick(System.Windows.Input.MouseButtonEventArgs e)
        {
            if (CanChangeWindowState)
            {
                Window owner = this.Owner;

                if (owner != null)
                {
                    WindowState state = owner.WindowState;

                    switch (state)
                    {
                        case WindowState.Maximized:
                        case WindowState.Minimized:
                            owner.WindowState = WindowState.Normal;
                            break;

                        case WindowState.Normal:
                            owner.WindowState = WindowState.Maximized;
                            break;
                    }
                }
            }

            base.OnMouseDoubleClick(e);
        }

        private void OnDragDelta(object sender, DragDeltaEventArgs e)
        {
            Window owner = this.Owner;

            if (owner != null)
            {
                owner.Left += e.HorizontalChange;
                owner.Top += e.VerticalChange;
                e.Handled = true;
            }
        }
    }
}
